package com.shanliao.controller;

import com.alibaba.fastjson.JSON;
import com.shanliao.respone.BaseResponse;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
@Slf4j
public class BaseController {

    protected ResponseEntity<String> responseData(BaseResponse<?> baseResponse) {
        String response = JSON.toJSONString(baseResponse);
        log.info("\n\n");
        log.info("返回结果: " + StringUtils.abbreviate(response, 30));
        log.info("\n\n");
        return new ResponseEntity<>(response, this.buildHttpHeaders(), HttpStatus.OK);
    }

    protected ResponseEntity<String> response(String responseStr){
        log.info("responseStr: " + responseStr);
        return new ResponseEntity<>(responseStr, this.buildHttpHeaders(), HttpStatus.OK);
    }


    private HttpHeaders buildHttpHeaders(){
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Content-Type", "application/json;charset=UTF-8");
        return httpHeaders;
    }
}
